package bocchi;

import basemod.AutoAdd;
import basemod.BaseMod;
import basemod.interfaces.*;
import bocchi.base.BaseCustomCard;
import bocchi.card.Strike;
import bocchi.character.Characters;
import bocchi.general.CardData;
import bocchi.general.DataLoader;
import bocchi.general.ResourcePath;
import bocchi.relic.HairPin;
import com.badlogic.gdx.Gdx;
import com.evacipated.cardcrawl.modthespire.lib.SpireInitializer;
import com.google.gson.Gson;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.localization.*;
import com.megacrit.cardcrawl.unlock.UnlockTracker;

import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@SpireInitializer
public class BocchiCore implements EditCardsSubscriber, EditStringsSubscriber, EditCharactersSubscriber, EditRelicsSubscriber, EditKeywordsSubscriber {
    private static final Gson gson = new Gson();

    public BocchiCore() {
        BaseMod.subscribe(this);
        Characters.registerColors();
    }

    public static void initialize() {
        new BocchiCore();
    }

    @Override
    public void receiveEditCards() {
        new AutoAdd("bocchi").packageFilter(Strike.class).setDefaultSeen(true).cards();

        List<String> registered = new LinkedList<>();

        for (AbstractCard card : BaseMod.getCustomCardsToAdd()) {
            registered.add(card.cardID);
        }

        for (AbstractCard card : BaseMod.getColorlessCardsToAdd()) {
            registered.add(card.cardID);
        }

        for (Map.Entry<String, CardData> entry : DataLoader.getCardDataMap().entrySet()) {
            if (registered.contains(getGameID(entry.getKey()))) {
                continue;
            }

            AbstractCard card = new BaseCustomCard(entry.getValue());
            BaseMod.addCard(card);
            UnlockTracker.unlockCard(card.cardID);
        }
    }

    @Override
    public void receiveEditStrings() {
        BaseMod.loadCustomStringsFile(CardStrings.class, ResourcePath.ZHS + "card.json");
        BaseMod.loadCustomStringsFile(CharacterStrings.class, ResourcePath.ZHS + "character.json");
        BaseMod.loadCustomStringsFile(PowerStrings.class, ResourcePath.ZHS + "power.json");
        BaseMod.loadCustomStringsFile(RelicStrings.class, ResourcePath.ZHS + "relic.json");
        BaseMod.loadCustomStringsFile(StanceStrings.class, ResourcePath.ZHS + "stance.json");
    }

    @Override
    public void receiveEditCharacters() {
        Characters.registerCharacter();
    }

    @Override
    public void receiveEditRelics() {
        BaseMod.addRelic(new HairPin(), HairPin.TYPE);
    }

    @Override
    public void receiveEditKeywords() {
        String json = Gdx.files.internal(ResourcePath.KEYWORD).readString(String.valueOf(StandardCharsets.UTF_8));
        Keyword[] keywords = gson.fromJson(json, Keyword[].class);
        if (keywords != null) {
            for (Keyword keyword : keywords) {
                BaseMod.addKeyword("bocchi", keyword.NAMES[0], keyword.NAMES, keyword.DESCRIPTION);
            }
        }
    }

    public static String getGameID(String ID) {
        return "bocchi:" + ID;
    }
}
